package org.kjtc.web;

import net.sf.json.JSONObject;
import org.kjtc.commom.CommonUtil;
import org.kjtc.commom.XmlSerializer;
import org.kjtc.entity.*;
import org.kjtc.mapper.TimerMapper;
import org.kjtc.service.LogService;
import org.kjtc.service.TimerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.*;

/**
 * 执行内容
 */
@Component
public class Task extends TimerTask {

    private TimerMapper taskMapper;
    private LogService logService;

    public Task(TimerMapper taskMapper,LogService logService) {
        this.taskMapper = taskMapper;
        this.logService = logService;

    }

    public void run(){
        logService.insertLog("推送每人工作任务开始","");
        File file = new File(CommonUtil.WECHAT_CONFIG_PATH);
        int access = 0;
        String forwarUrl = "";
        String pushUrl = "";
        String wechatModuleID = "";
        String statisticsTime = "";
        if (file.exists()) {
            try {
                XmlSerializer<WeChatConfig> xSerializer = new XmlSerializer<WeChatConfig>();
                access = xSerializer.unMarshallXml(file, WeChatConfig.class).getAccess();
                forwarUrl = xSerializer.unMarshallXml(file, WeChatConfig.class).getForwardUrl();
                pushUrl = xSerializer.unMarshallXml(file, WeChatConfig.class).getPushUrl();
                wechatModuleID = xSerializer.unMarshallXml(file, WeChatConfig.class).getWeChatModuleID();
                statisticsTime = xSerializer.unMarshallXml(file, WeChatConfig.class).getStatisticsTime();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (access == 1) {
            List<BackLog> listMember = taskMapper.getAllMember();
            for(BackLog b:listMember){
                List<BackLog> list = taskMapper.getAllWorkData(b.getUserName());
                if (list.size() > 0) {
                    Map<String, Object> dataMap = new HashMap<String, Object>();
                    Map<String, Object> requestMap = new HashMap();
                    dataMap.put("first", "{value: \"" + list.get(0).getUserName() + "\", color: \"#FF0000\"}");
                    String keyWord0 = "";
                    for (int i = 0;i < list.size();i++) {
                        BackLog backLog = list.get(i);
                        String tmp = "";
                        if(i == 0){
                            tmp = "\n"+ backLog.getProductName() + "-" + backLog.getBackLogName() + "\n";
                        }else if (i == list.size() - 1) {
                            tmp = backLog.getProductName() + "-" + backLog.getBackLogName();
                        } else {
                            tmp = backLog.getProductName() + "-" + backLog.getBackLogName() + "\n";
                        }
                        keyWord0 += tmp;
                    }
                    JSONObject json = new JSONObject();
                    json.put("value", keyWord0);
                    json.put("color", "#0000FF");
                    dataMap.put("keyword1", json);
                    JSONObject json1 = new JSONObject();
                    json1.put("value", "项目管理");
                    json1.put("color", "#0000FF");
                    dataMap.put("keyword2", json1);
                    JSONObject json2 = new JSONObject();
                    json2.put("value", "PM");
                    json2.put("color", "#0000FF");
                    dataMap.put("keyword3", json2);
                    requestMap.put("sendFrom", "项目管理");
                    requestMap.put("url", forwarUrl);
                    requestMap.put("templateID", wechatModuleID);
                    requestMap.put("userID","'" + b.getUserID()+ "'");
                    requestMap.put("data", dataMap);
                    try{
                        JSONObject jsonObject = Tools.requestHttpUrl(pushUrl, requestMap);
                        JSONObject job = null;
                        String resultSuccessUser = null;
                        if(jsonObject.size() > 0){
                            job = jsonObject.getJSONObject("result");
                            if(job.getString("successUser") != null){
                                logService.insertLog(b.getUserName()+"的工作任务推送成功...","");
                            }else{
                                if(jsonObject.getString("TemplateIDEmpty") != null){
                                    logService.insertLog(b.getUserName()+"的工作任务推送失败(消息模板为空)...","");
                                }else if(jsonObject.getString("UserIDEmpty") != null){
                                    logService.insertLog(b.getUserName()+"的工作任务推送失败(消息模板为空)...","");
                                }else if(jsonObject.getString("DataEmpty") != null){
                                    logService.insertLog(b.getUserName()+"的工作任务推送失败(发送消息内容为空)...","");
                                }else if(jsonObject.getString("TokenError") != null){
                                    logService.insertLog(b.getUserName()+"的工作任务推送失败(获取微信token失败)...","");
                                }else if(jsonObject.getString("NotExistUser") != null){
                                    logService.insertLog(b.getUserName()+"的工作任务推送失败(发送用户不存在)...","");
                                }else if(jsonObject.getString("Empty") != null){
                                    logService.insertLog(b.getUserName()+"的工作任务推送失败(发送数据为空)...","");
                                }else if(job.getString("failUser") != null){
                                    logService.insertLog(b.getUserName()+"的工作任务推送失败...","");
                                }
                            }
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }
        logService.insertLog("推送每人工作任务结束","");
    }

}
